package bcared;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CheckInOut {
	
	private int checkID;
	private Engineer engineer;
	private int type;
	private String logTime;
	private String actionNotes;
	
	private ResultSet rs = null;
	private CallableStatement csmt = null;
	private PreparedStatement ps = null;
	private OraDB myDB = new OraDB();
	
	public CheckInOut(int engineerID, int type, String actionNotes, boolean resolved) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
		this.engineer = new Engineer(engineerID);
		int soid = engineer.getSOID();
		
		myDB.createConnection();
		
		if (type == 1) {
			String myQuery = "INSERT INTO CheckInOut (checkID, engineerID, type, logTime, actionNotes)"
					 + " VALUES((select (max(checkId) + 1) as checkID from checkinout), ?, ?, current_date, ?)";
			
			ps = myDB.conn.prepareStatement(myQuery);
			ps.setInt(1, this.engineer.getEngineerID());
			ps.setInt(2, type);
			ps.setString(3, actionNotes);
			rs = ps.executeQuery();
			
			myQuery = "INSERT INTO ServiceOrdersCheck (SOID, checkID)"
				+ " VALUES(?, (select max(checkID) as checkID from checkinout))";
			
			ps = myDB.conn.prepareStatement(myQuery);
			ps.setInt(1, soid);

			rs = ps.executeQuery();
			
		} else {
			
			if (resolved) {
				String myQuery = "INSERT INTO CheckInOut (checkID, engineerID, type, logTime, actionNotes)"
						+ " VALUES((select (max(checkId) + 1) as checkID from checkinout), ?, ?, current_date, '')";
				
				ps = myDB.conn.prepareStatement(myQuery);
				ps.setInt(1, this.engineer.getEngineerID());
				ps.setInt(2, type);
				rs = ps.executeQuery();
				
				ServiceOrder so = new ServiceOrder(soid);
				so.setResolutionNotes(actionNotes);
				so.setSoStatusID(1);
				so.update();
				Equipment e = new Equipment(so.getEquipmentID());
				e.setStatusID(1);
				
				this.engineer.clearSOID();
			
			} else {
				String myQuery = "INSERT INTO CheckInOut (checkID, engineerID, type, logTime, actionNotes)"
						+ " VALUES((select (max(checkId) + 1) as checkID from checkinout), ?, ?, current_date, ?)";

				ps = myDB.conn.prepareStatement(myQuery);
				ps.setInt(1, this.engineer.getEngineerID());
				ps.setInt(2, type);
				ps.setString(3, actionNotes);
				rs = ps.executeQuery();
			}

			String myQuery2 = "INSERT INTO ServiceOrdersCheck (SOID, checkID)"
					+ " VALUES(?, (select max(checkID) as checkID from checkinout))";

			ps = myDB.conn.prepareStatement(myQuery2);
			ps.setInt(1, soid);

			rs = ps.executeQuery();
			
			myQuery2 = "select max(checkID) as checkID from checkinout";
			ps = myDB.conn.prepareStatement(myQuery2);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				this.checkID = rs.getInt("checkID");
			}
			
			myDB.closeConnection();

		}
		
		populate();
		
	}
	
	public CheckInOut(int checkID) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
		this.checkID = checkID;
		populate();
	}
	
	public void populate() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
		myDB.createConnection();
		String myQuery = "select * from checkinout where checkid = ?";
		
		this.ps = myDB.conn.prepareStatement(myQuery);
		this.ps.setInt(1, this.checkID);
		this.rs = this.ps.executeQuery();
		
		while (rs.next()) {
			this.checkID = rs.getInt("checkID");
			this.engineer = new Engineer(rs.getInt("engineerID"));
			this.type = rs.getInt("type");
			this.logTime = rs.getString("logTime");
			this.actionNotes = rs.getString("actionNotes");
		}
		
	}

	public int getCheckID() {
		return checkID;
	}

	public Engineer getEngineer() {
		return engineer;
	}

	public int getType() {
		return type;
	}

	public String getLogTime() {
		return logTime;
	}

	public String getActionNotes() {
		if (actionNotes == null) {
			return "";
		}
		return actionNotes;
	}

	public void setEngineer(Engineer engineer) {
		this.engineer = engineer;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setLogTime(String logTime) {
		this.logTime = logTime;
	}

	public void setActionNotes(String actionNotes) {
		this.actionNotes = actionNotes;
	}
}
